package G.二叉搜索树;

public class _783_二叉搜索树节点最小距离 {
    /**
     * 同530题
     * 思路：中序遍历 temp存上一个的值 ans存答案的数值
     * @param root
     * @return
     */

    int temp = Integer.MAX_VALUE;
    int ans = Integer.MAX_VALUE;
    public int minDiffInBST(TreeNode root) {

        helper(root);
        return ans;
    }

    //用于中旭遍历 更新ans
    public void helper(TreeNode node){

        if (node == null){
            return;
        }

        helper(node.left);

        if (node.left==null){
            temp = node.val;

        }else {
            ans = Math.abs(node.val-temp) < ans?Math.abs(node.val-temp):ans;
            temp = node.val;
        }


        helper(node.right);


    }
}
